package com.hzjh.base.style.view.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import android.view.Gravity
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.hzjh.base.style.R
import com.hzjh.base.style.databinding.DialogConfirmCancelBinding
import androidx.core.view.updateLayoutParams

class ConfirmDialog (context: Context?, themeResId: Int) : Dialog(context!!, themeResId) {
    class Builder(private val context: Context) {
        private var title: String? = null
        private var scene: Int = 0
        private var message: String? = null
        private var positiveButtonContent: String? = null
        private var negativeButtonContent: String = ""
        private var positiveButtonListener: DialogInterface.OnClickListener? = null
        private var negativeButtonListener: DialogInterface.OnClickListener? = null
        private var contentView: View? = null
        private var contentTestColor: Int = 0
        private var withOffSize: Float = 0.toFloat()
        private var heightOffSize: Float = 0.toFloat()
        private var contentIsCenter: Boolean = true
        private var canceledOnTouchOutside: Boolean = true
        private var canCancel: Boolean = true

        private var btnOrientation = LinearLayout.HORIZONTAL

        /**
         * 对话框取消/确认按钮方向
         * @param [orientation][LinearLayout.HORIZONTAL]
         */
        fun setBtnOrientation(btnOrientation: Int): Builder {
            this.btnOrientation = btnOrientation
            return this
        }


        /**
         * 是否可以关闭dialog 如返回键 点外边
         * false 不能关闭
         * true 可以关闭 默认true
         * 这个优先级最高
         */
        fun setCanCancel(canCancel: Boolean): Builder {
            this.canCancel = canCancel
            return this
        }

        /**
         * 隐藏取消按钮
         */
        fun hideCanCancelBtn(): Builder {
            negativeButtonContent = ""
            return this
        }


        /**
         * 是否点击外部区域关闭dialog
         * false 不能关闭
         * true 可以关闭 默认true
         */
        fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean): Builder {
            this.canceledOnTouchOutside = canceledOnTouchOutside
            return this
        }

        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setScene(scene: Int): Builder {
            this.scene = scene
            return this
        }

        fun setContentIsCenter(contentIsCenter: Boolean): Builder {
            this.contentIsCenter = contentIsCenter
            return this
        }


        fun setTitle(title: Int): Builder {
            this.title = context.getText(title) as String
            return this
        }

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        fun setMessageColor(color: Int): Builder {
            this.contentTestColor = color
            return this
        }


        fun setSureButton(text: String, listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonContent = text
            this.positiveButtonListener = listener
            return this
        }

        fun setSureButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonContent = context.getText(textId) as String
            this.positiveButtonListener = listener
            return this
        }

        fun setCancelButton(text: String, listener: DialogInterface.OnClickListener?): Builder {
            this.negativeButtonContent = text
            this.negativeButtonListener = listener
            return this
        }

        fun setCancelButton(textId: Int, listener: DialogInterface.OnClickListener?): Builder {
            this.negativeButtonContent = context.getText(textId) as String
            this.negativeButtonListener = listener
            return this
        }

        fun setContentView(v: View): Builder {
            this.contentView = v
            return this
        }

        fun setWith(v: Float): Builder {
            this.withOffSize = v
            return this
        }

        fun setContentView(v: Float): Builder {
            this.heightOffSize = v
            return this
        }

        fun create(): ConfirmDialog {
            /**
             * 初始化Dialog
             */
            val dialog = ConfirmDialog(
                context,
                R.style.custom_dialog
            )

            /**
             * 初始化Dialog的布局页面
             */

            val vBinding: DialogConfirmCancelBinding =
                DialogConfirmCancelBinding.inflate(LayoutInflater.from(context), null, false)

            if (btnOrientation == LinearLayout.VERTICAL) {
                vBinding.layoutBtn.orientation = btnOrientation
                vBinding.btnCancel.updateLayoutParams {
                    width = ViewGroup.LayoutParams.MATCH_PARENT
                }
                vBinding.btnSure.updateLayoutParams {
                    width = ViewGroup.LayoutParams.MATCH_PARENT
                }
            }

            if (!TextUtils.isEmpty(title)) {
                vBinding.title.text = title
            } else {
                vBinding.title.visibility = View.GONE
            }

            if (contentTestColor != 0) {
                vBinding.content.setTextColor(contentTestColor)
            }

            if (!TextUtils.isEmpty(message)) {
                vBinding.content.text = message
                if (!contentIsCenter) {
                    vBinding.content.gravity =
                        Gravity.LEFT
                }
            } else {
                vBinding.content.visibility =
                    View.GONE
            }

            if (!TextUtils.isEmpty(positiveButtonContent)) {
                vBinding.btnSure.text =
                    positiveButtonContent
                if (positiveButtonListener != null) {
                    vBinding.btnSure
                        .setOnClickListener { positiveButtonListener!!.onClick(dialog, -1) }

                }
            } else {
                vBinding.btnSure.visibility =
                    View.GONE
            }

            if (!TextUtils.isEmpty(negativeButtonContent)) {
                vBinding.btnCancel.text =
                    negativeButtonContent
                if (negativeButtonListener != null) {
                    vBinding.btnCancel
                        .setOnClickListener { negativeButtonListener!!.onClick(dialog, -2) }
                } else {
                    vBinding.btnCancel
                        .setOnClickListener {
                            dialog.dismiss()
                        }
                }
            } else {
                vBinding.btnCancel.visibility =
                    View.GONE
                vBinding.vBtnGap.visibility =
                    View.GONE
            }
            /**
             * 将初始化完整的布局添加到dialog中
             */
            dialog.setContentView(vBinding.root)


            if (canCancel) {
                /**
                 * 禁止点击Dialog以外的区域时Dialog消失
                 */
                dialog.setCanceledOnTouchOutside(canceledOnTouchOutside)
            } else {
                /**
                 * 禁用键盘搜索键
                 * https://blog.csdn.net/lk512893708/article/details/51997811
                 */
                dialog.setOnKeyListener { _, keyCode, _ -> keyCode == KeyEvent.KEYCODE_SEARCH }
                dialog.setCancelable(false)
            }

            val window = dialog.window
            val context = this.context as Activity
            val windowManager = context.windowManager

            val defaultDisplay = windowManager.defaultDisplay

            val attributes = window!!.attributes

            if (withOffSize.toDouble() != 0.0) {

                attributes.width = (defaultDisplay.width * withOffSize).toInt()
            } else {
                attributes.width = (defaultDisplay.width * 0.77).toInt()

            }
            if (heightOffSize.toDouble() != 0.0) {

                attributes.height = (defaultDisplay.height * heightOffSize).toInt()
            }
            window.attributes = attributes
            return dialog
        }
    }
}